<?php
/**
热门话题
 */

namespace App\Http\Controllers\Property;

use App\Common\json;
use App\Common\SmsClient;
use App\Http\Controllers\Controller;
use App\Libraries\upload;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class HottopicController extends Controller{

    //获取全部热门话题
    public function AllHottopic(Request $request){
        if($request ->isMethod('post')){
            $village_id    = $request ->input('village_id');
            $data     = DB::table('hottopic')
                      ->select('hottopic_id','hottopic_title','hottopic_image','hottopic_create_time')
                      ->where('hottopic_village',$village_id)
                      ->get();
            if($data == true){
                return json::json('success', '获取热门话题成功', $data);
            }else{
                return json::json('failure','由于网络或者不正当原因失败',json::value(2));
            }
        }
    }

    //热门话题
    public  function primary($release_id){
        $data = DB::table('hottopic')
              ->select('hottopic_id','hottopic_title','hottopic_content','hottopic_image')
              ->where('hottopic_id',$release_id)
              ->get();
        return $data;
    }

    //跟贴内容
    public  function secondary($release_id){
        $data = DB::table('hottopicreply')
            ->leftjoin('user','hottopicreply.user_hottopicreply','=','user.user_id')
            ->where('hottopicreply_hottopic',$release_id)
            ->orderby('hottopicreply_creation_time','ASE')
            ->get();
        return $data;
    }

    //通过ID获取详情
    public function SelectHottopic(Request $request){
        if($request ->isMethod('post')){
            $hottopic_id       = $request ->input('hottopic_id');
            $data              = $this ->primary($hottopic_id);
            foreach ($data as $value){
                $id     = $value ->hottopic_id;
                $hottopic_title =$value ->hottopic_title;
                $hottopic_content = $value ->hottopic_content;
                if(empty($value ->hottopic_image)){
                    $hottopic_image  = [];
                }else {
                    $hottopic_image = explode(',', $value ->hottopic_image);
                }
                $particulars ="http://www.mukitech.com/wuye/app/public/property/Hottopic/$hottopic_id";
               // header("Content-Type: text/html; charset=utf-8");
               // $html_content = file_get_contents($particulars);
               // $H5               = file_get_contents()
                $secondary        = $this ->secondary($id);
                foreach ($secondary as $key){
                    $head      = $key ->head;
                   $nickname  = $key ->nickname;
                    $content    = $key ->content;
                    $time   = $key  ->hottopicreply_creation_time;
                    $lk[] = array('head'=>$head,'nickname'=>$nickname,'content'=>$content,'time'=>$time);
                }
                if(empty($lk)) {
                    $da = array('hottopic_title' => $hottopic_title, 'hottopic_content' => $hottopic_content, 'hottopic_image' => $hottopic_image,'H5'=>$particulars);
                }else{
                    $da = array('hottopic_title' => $hottopic_title, 'hottopic_content' => $hottopic_content, 'hottopic_image' => $hottopic_image,
                        'H5'=>$particulars,   'list'=>$lk);
                }
                }
            if($data == true){
                return json::json('success', '修改新住户成功', $da);
            }else{
                return json::json('failure','由于网络或者不正当原因失败',json::value(1));
            }
        }
    }

    //发布评论
    public function ReplyHottopic(Request $request){
        if($request ->isMethod('post')){
            $arr = (object)array();
            $user_id      = $request ->input('user_id');
            $hottopic_id  = $request ->input('hottopic_id');
            $content      = $request ->input('content');
            $select       = DB::table('hottopicreply')
                          ->where('user_hottopicreply',$user_id)
                          ->where('hottopicreply_hottopic',$hottopic_id)
                          ->where('content',$content)
                          ->get();
            if($select == true){
                return json::json('success', '你发表的评论已存在', json::value(1));
            }else{
                $data     = DB::table('hottopicreply')
                          ->insert([
                              'user_hottopicreply'   =>$user_id,
                              'hottopicreply_hottopic'   => $hottopic_id,
                              'content'                    => $content,
                              'hottopicreply_creation_time' =>date('Y-m-d H:i:s')
                          ]);
                if($data == true){

                    return json::json('success', '发布评论成功',$arr);
                }else{
                    return json::json('failure','由于网络或者不正当原因失败',$arr);
                }
            }
        }
    }

    //删除
    public function DelHottopic(Request $request){
        if($request ->isMethod('post')){
            $arr = (object)array();
            $hottopicreply_id    = $request ->input('hottopicreply_id');
            $data                = DB::table('hottopicreply')
                                 ->where('hottopicreply_id',$hottopicreply_id)
                                 ->delete();
            if($data == true){
                return json::json('success', '删除评论成功', $arr);
            }else{
                return json::json('failure','由于网络或者不正当原因失败',$arr);
            }
        }
    }

}